home *** CD-ROM | disk | FTP | other *** search
/ com!online 2002 July / com!online0702.iso / software / livemotion / DATA1.CAB / Scripting_Resources / Samples / Automation_Scripts / Event Handlers.js < prev    next >
Encoding:
Text File  |  2002-05-13  |  5.1 KB  |  136 lines

  1. //////////////////////////////////////////////////
  2. //
  3. // ADOBE SYSTEMS INCORPORATED 
  4. // Copyright 2002 Adobe Systems Incorporated 
  5. // All Rights Reserved 
  6. //
  7. // NOTICE:  Adobe permits you to use, modify, and 
  8. // distribute this file in accordance with the terms
  9. // of the Adobe license agreement accompanying it.  
  10. // If you have received this file from a source 
  11. // other than Adobe, then your use, modification,
  12. // or distribution of it requires the prior 
  13. // written permission of Adobe. 
  14. //
  15. //////////////////////////////////////////////////
  16.  
  17. //////////////////////////////////////////////////
  18. // 
  19. // Event Handlers.js
  20. //
  21. // DESCRIPTION
  22. //
  23. // This script shows how to add script to the event handlers 
  24. // in the player script. This allows you to write event based
  25. // script in your programs.
  26. //
  27. // HOW TO USE
  28. //
  29. // Create three objects in the composition and run the script.
  30. // Note: You may create more objects in the composition and 
  31. // change the loop controls as number of objects -1 accordingly, as
  32. // you would do for the objects array of composition.
  33. // 
  34. // Select Automation > Run Automation Scripts > Event Handlers.js
  35. //  
  36. // Preview the result by switching to the preview mode
  37. // or export the file and view it in the browser
  38. //////////////////////////////////////////////////
  39.  
  40. // Main Code [Execution of script begins here]
  41.  
  42. // Check if any composition is open
  43. if(application.compositions.length > 0){
  44.     comp = application.currentComposition;
  45.     if(comp.selection.length >= 3){ // Checks if at least one object is selected
  46.         var objs = comp.selection; // Store all selected objects in the array objs
  47.         setScript(comp, objs); // Call function setScript
  48.     }
  49.     else{ // If no objects are selected brings the Console window up 
  50.         Console.show();
  51.         // Writes to the Console
  52.         Console.write("\nPlease select at least 3 objects to apply the Animation and run the script again.\n");
  53.     }
  54. }
  55. else{// if no composition open
  56.     // opens a new composition
  57.     comp = application.newComposition();
  58.     Console.show();
  59.     Console.write("\nNew Composition opened\nPlease create and select at least 3 objects to apply the Animation and run the script again.\n");
  60. }
  61.  
  62. // Add your own functions here
  63.  
  64. //////////////////////////////////////////////////
  65. // 
  66. // setScript:
  67. //
  68. // Demonstrates how user can add scripts to the different event 
  69. // handlers available in LiveMotion application. Also shows 
  70. // a way to access all the objects in the composition through 
  71. // player script. Adding scripts to event handlers help you to 
  72. // do event based programming. 
  73. //
  74. // comp: The currently open and active file/composition.
  75. // objs: All the selected objects in the composition.
  76. //
  77. //////////////////////////////////////////////////
  78.  
  79. function setScript(comp, objs)
  80. {
  81.     // Loop for naming all the objects in the composition 
  82.     // and converting them into movieclips
  83.     for(i=0; i < objs.length ; i++){
  84.         // Name all objects on the composition
  85.         objs[i].name = "object"+i; 
  86.         
  87.         // Convert object into MovieClip
  88.         objs[i].isMovieClip = true; 
  89.     }
  90.     
  91.     // Note: After we convert objects to movie clips we can use 
  92.     // any of the movie clip properties/functions to manipulate 
  93.     // or animate the objects.
  94.         
  95.     // Assign script for the onLoad event handler to the variable script1.
  96.     script1 = 'var j=0;\n';
  97.     script1 += 'var numy = _root._height / 2; // height of the composition \n';
  98.     script1 += 'var numx = _root._width / 2; // width of the composition\n';
  99.     
  100.     // Loop for positioning objects in a queue with difference of 
  101.     // their heights only so that one does not overlap over the other.
  102.     script1 += 'for(var i=0; i < 3; i++){\n'; 
  103.     script1 += '\t// Position all objects on composition\n';
  104.     script1 += '\t_root[\"object\"+i]._x = 0; \n'; 
  105.     script1 += '\t//Difference between positioning objects is the height of each object \n';
  106.     script1 += '\t_root[\"object\"+i]._y = _root[\"object\"+i]._height;\n} \n';
  107.     
  108.     
  109.     // This script in variable script2 is for the onEnterFrame event handler.
  110.     script2 = 'angle = j*(Math.PI/180); // the angle for the circle\n'; 
  111.     
  112.     // Loop through all objects in the composition.
  113.     script2 += 'for(var i=0; i < 3; i++){\n'; 
  114.     script2 += '\t// 3 is the num of objects\n';
  115.  
  116.     // Setting the x position through the movieclips property - ._x 
  117.     // of the objects to make them move in a circle.
  118.     script2 += '\t_root[\"object\"+i]._x = 95*Math.sin(angle+(i*2*Math.PI/8))+numx;\n';
  119.     script2 += '\t// Formula to calculate x position of object to move them in a circle\n';
  120.         
  121.     // Setting the y position through the movieclips property - ._y 
  122.     // of the objects to make them move in a circle.
  123.     script2 += '\t_root[\"object\"+i]._y = 95*Math.cos(angle+(i*2*Math.PI/8))+numy;\n';
  124.     script2 += '\t// Formula to calculate y position of object to move them in a circle\n}' ;
  125.     script2 += 'j++;\n';
  126.     
  127.     // Set the script on the specific handlers.
  128.     comp.setHandlerScript(LMHandlerType.onLoad, script1);
  129.     comp.setHandlerScript(LMHandlerType.onEnterFrame, script2);
  130.     
  131.     // Note: A similar getHandlerScript method is also available to get any
  132.     // player script on any handler. This could be more useful in LiveTabs.
  133.     return;
  134. }
  135.  
  136.